var orderStates;
var userMess="";
var page=1;
$(function(){
    //获取用户信息
    getUser();
    userMess=JSON.stringify(ym.user);
    var orderState=getUrlVal("orderState");
    if(orderState==undefined||orderState==null||orderState==""){
        orderState=0;
    };
    getOrderLi(orderState,1);
   $(".orderNavLi").click(function(){
       $("#moreLi").addClass("hide");
       $("#moreLi").addClass("moreLis");
       $("#moreLi").html("加载更多");
       $(this).addClass("activeOrder").siblings("li").removeClass("activeOrder");
       if($(this).html()=="全部"){
           orderStates=0;
       }else if($(this).html()=="待付款"){
           orderStates=1;
       }else if($(this).html()=="待发货"){
           orderStates=2;
       }else if($(this).html()=="待收货"){
           orderStates=3;
       }else if($(this).html()=="已完成"){
           orderStates=4;
       };
       $(".allOrderUl").html("");
       getOrderLi(orderStates,1)
   })
    //加载更多
    $(".moreLi").click(function(){
        console.log(orderStates);
        if($(this).hasClass("moreLis")){
            page++;
            getOrderLi(orderStates,page);
        }
    })
});
function getOrderLi(orderState,page){
    $("#loadingModal").modal();
    $(".orderNavLi").each(function(){
        if($(this).index()==orderState){
           $(this).addClass("activeOrder").siblings("li").removeClass("activeOrder");
        }
    })
    var shopOrderStatus="";
    if(orderState==0){
        shopOrderStatus="";
    }else if(orderState==1){//待付款
        shopOrderStatus="pendingPayment";
    }else if(orderState==2){//待发货
        shopOrderStatus="pendingDelivery";
    }else if(orderState==3){//待收货
        shopOrderStatus="pendingGoods";
    }else if(orderState==4){//已完成
        shopOrderStatus="theEnd";
    }
    ym.ajax({
        url : "/shop/order/getAll",
        type : "post",
        param:{
            shopOrderStatus:shopOrderStatus,
            pageNumber:page,
            ymUser:userMess,
        },
        success : function(data) {
            console.log(data);
            console.log("获取订单信息")
            var html="";
            if(data.obj!=null&&data.obj.length>0){
                $("#moreLi").removeClass("hide");
                for(var i=0;i<data.obj.length;i++){
                    var l=data.obj[i];
                    html+='<li>'
                    if(l.shopOrderStatus=="pendingPayment"){//待付款
                        html+='<span class="orderState">等待付款</span>'
                    }else if(l.shopOrderStatus=="pendingDelivery"){//待发货
                        html+='<span class="orderState">等待发货</span>'
                    }else if(l.shopOrderStatus=="pendingGoods"){//待收货
                        html+='<span class="orderState">等待收货</span>'
                    }else if(l.shopOrderStatus=="theEnd"){//已完成
                        html+='<span class="orderState">已完成</span>'
                    }
                    html+='<span class="orderNum">订单编号：'+l.shopOrderFlowno+'</span>'
                    var arrPro=[];
                    arrPro=l.orderProductVos;
                    for(var j=0;j<arrPro.length;j++){
                        var k=arrPro[j];
                        html+='<span class="goodsDiv" onclick="getOrderDetails('+l.shopOrderId+')">'
                        html+='<span class="goodsImgDiv">'
                        html+='<img src="'+k.proPic+'" class="goodsImg" />'
                        html+='</span>'
                        html+='<span class="goodsMessDivs">'
                        html+='<span class="goodName">'
                        html+='<span class="goodsName">'+k.name+'</span>'
                        html+='</span>'
                        html+='<span class="goodsNumDiv">'
                        if(k.payMethod==0){//0积分，1金钱
                            html+='<span class="goodMoneyDiv ar-left">'+k.point+'积分</span>'
                        }else if(k.payMethod==1){
                            var price=(k.price*0.01).toFixed(2);
                            html+='<span class="goodMoneyDiv ar-left">'+price+'（'+k.point+'积分）</span>'
                        }
                        html+='<span class="goodsNum ar-right">X '+k.number+'</span>'
                        html+='</span></span></span>'
                    }
                    html+='<span class="moneyDiv">'
                    html+='<span class="orderTime">'+l.showOrderTime+'</span>'
                    if(l.payType==0){//0积分,1金钱
                        html+='<span class="endMoney">实付：'+l.shopOrderPoint+'积分</span>'
                    }else if(l.payType==1){
                        var total=(l.shopOrderTotalcost*0.01).toFixed(2);
                        if(l.shopOrderStatus=="pendingPayment"){
                            html+='<span class="endMoney">应付： ￥ '+total+'+'+l.shopOrderPoint+'积分</span>'
                        }else{
                            html+='<span class="endMoney">实付： ￥ '+total+'+'+l.shopOrderPoint+'积分</span>'
                        }
                    }
                    html+='</span>'
                    if(l.shopOrderStatus=="pendingPayment"){//待付款
                        html+='<span class="operBtn">'
                        html+='<button class="hasColorBtn btnBackCor" onclick="goBuy('+l.shopOrderId+','+total+','+l.shopOrderPoint+')">去付款</button>'
                        html+='<button class="noColorBtn" onclick="delOrder('+l.shopOrderId+',0)">取消订单</button>'
                        html+='</span>'
                    }else if(l.shopOrderStatus=="pendingDelivery"){//待发货
                        html+='<span class="operBtn">'
                        html+='<button class="noColorBtn">待发货</button>'
                        html+='</span>'
                    }else if(l.shopOrderStatus=="pendingGoods"){//待收货
                        html+='<span class="operBtn">'
                        html+='<button class="hasColorBtn btnBackCor" onclick="delOrder('+l.shopOrderId+',1)">确认收货</button>'
                        html+='</span>'
                    }else if(l.shopOrderStatus=="theEnd"){//已完成
                        html+='<span class="operBtn">'
                        html+='<button class="noColorBtn" onclick="delOrder('+l.shopOrderId+',0)">删除订单</button>'
                        html+='</span>'
                    }
                    html+='<div class="lationBelt"></div></li>'
                }
            }else{
                if(page!=1){
                    $("#moreLi").removeClass("hide");
                    $("#moreLi").removeClass("moreLis");
                    $("#moreLi").html("没有更多啦~");
                }else{
                    $("#moreLi").addClass("hide");
                    html+='<li><span class="noOrder">暂无相关订单信息！</span></li>'
                }
            }
            $(".allOrderUl").append(html);
            $("#loadingModal").modal('close');
        }
    })
}
//查看详情
function getOrderDetails(orderId){
    window.location.href="orderDetails.html?orderId="+orderId;
}
//操作订单
function delOrder(orderId,type){
    if(type==0){//删除
        $('#myConfirm .am-modal-bd').html("您是否确定删除此订单？");
        var update="";
    }else if(type==1){
        $('#myConfirm .am-modal-bd').html("您是否确认收货？");
        var update=2;
    }
    var $confirm = $('#myConfirm');
    var confirm = $confirm.data('amui.modal');
    var onConfirm = function() {
        //点击确认按钮调用事件
        ym.ajax({
            url:'/shop/order/del',
            param:{
                orderId:orderId,
                update:update,
            },
            success:function(data){
                console.log(data)
                if(data.res==1){
                    $("#alertModal .am-modal-bd").html("操作成功！");
                    $("#alertModal").modal();
                    setTime();
                    getOrderLi(orderStates,1);
                }
            }
        });

    };
    var onCancel = function() {
        //取消按钮调用事件
    };
    if (confirm) {
        confirm.options.onConfirm =  onConfirm;
        confirm.options.onCancel =  onCancel;
        confirm.toggle(this);
    } else {//重定义模态窗函数
        $confirm.modal({
            relatedElement: this,
            closeViaDimmer: false,//禁止点击遮罩层关闭
            onConfirm: onConfirm,
            onCancel: onCancel
        });
    }
}
/*两秒后关闭弹窗*/
function setTime(){
    setTimeout(function () {
        $("#alertModal").modal('close');
    }, 2000);
}
//去付款
function goBuy(orderId,totalFee,allIntearge){
    var nums=0;
    $('#myConfirm .am-modal-bd').html("您是否确认花费"+totalFee+"元+"+allIntearge+"积分购买此商品？");
    var $confirm = $('#myConfirm');
    var confirm = $confirm.data('amui.modal');
    var onConfirm = function() {
        nums++;
        $(".masks").removeClass("hide");
        if(nums==1){
            totalFee *= 100;
            //调支付接口
            payType(orderId,totalFee);
        }
    };
    var onCancel = function() {
        //取消按钮调用事件
    };
    if (confirm) {
        confirm.options.onConfirm =  onConfirm;
        confirm.options.onCancel =  onCancel;
        confirm.toggle(this);
    } else {//重定义模态窗函数
        $confirm.modal({
            relatedElement: this,
            closeViaDimmer: false,//禁止点击遮罩层关闭
            onConfirm: onConfirm,
            onCancel: onCancel
        });
    }
}
